#!/usr/bin/env php
<?php
/**
 * +----------------------------------------------------------------------
 * | swoolefy framework bases on swoole extension development, we can use it easily!
 * +----------------------------------------------------------------------
 * | Licensed ( https://opensource.org/licenses/MIT )
 * +----------------------------------------------------------------------
 * | @see https://github.com/bingcool/swoolefy
 * +----------------------------------------------------------------------
 */

use Swoolefy\Cmd\MonitorCmd;
use Swoolefy\Cmd\ReloadCmd;
use Swoolefy\Cmd\SendCmd;
use Swoolefy\Cmd\StartCmd;
use Swoolefy\Cmd\CreateCmd;
use Swoolefy\Cmd\StatusCmd;
use Swoolefy\Cmd\StopCmd;
use Swoolefy\Cmd\RestartCmd;
use Symfony\Component\Console\Application;

function isWorkerService(): bool
{
    if (isDaemonService() || isScriptService() || isCronService()) {
        return true;
    }
    return false;
}

function isCliService()
{
    return !isWorkerService();
}

function isDaemonService(): bool
{
    if (defined('IS_DAEMON_SERVICE') && IS_DAEMON_SERVICE == 1) {
        return true;
    }
    return false;
}

function isCronService(): bool
{
    if (defined('IS_CRON_SERVICE') && IS_CRON_SERVICE == 1) {
        return true;
    }
    return false;
}

function isScriptService(): bool
{
    if (defined('IS_CLI_SCRIPT') && IS_CLI_SCRIPT == 1) {
        return true;
    }
    return false;
}

function isDaemon(): bool
{
    $daemon = getenv('daemon');
    if (empty($daemon)) {
        return false;
    }
    return true;
}

function loadGlobalConf()
{
    return \Swoolefy\Core\SystemEnv::loadGlobalConf();
}

function parseScriptPidFile(string $pidFile): string
{
    if (isScriptService()) {
        if (!$pidFile) {
            throw new InvalidArgumentException('The provided pid file path is empty or invalid.');
        }
        $pathInfo = pathinfo($pidFile);
        if (!is_numeric(WORKER_PORT)) {
            throw new RuntimeException('The WORKER_PORT is not a valid number.');
        }
        $pidFile = sprintf('%s%s%s_%s.pid', $pathInfo['dirname'], DIRECTORY_SEPARATOR, $pathInfo['filename'], WORKER_PORT);
    }
    return $pidFile;

}

function showLogo()
{
    $logo =
        <<<LOGO
  ______                                _           _ _ _ _
 /  ____|                              | |         |  _ _ _|  _   _
|  (__     __      __   ___     ___    | |   ___   | |       | | | |
 \___  \   \ \ /\ / /  / _ \   / _ \   | |  / _ \  | |_ _ _  | | | |
 ____)  |   \ V  V /  | (_) | | (_) |  | | | ___/  |  _ _ _| | |_| |
|_____ /     \_/\_/    \___/   \___/   |_|  \___|  | |        \__, |
                                                   |_|           | |
                                                              __ / |
                                                             |_ _ /
LOGO;
    initConsoleStyleIo()->write("<info>$logo</info>", true);
}

(function () {
    $application = new Application();
    $application->add(new CreateCmd());
    $application->add(new StartCmd());
    $application->add(new StopCmd());
    $application->add(new StatusCmd());
    $application->add(new ReloadCmd());
    $application->add(new SendCmd());
    $application->add(new MonitorCmd());
    $application->add(new RestartCmd());
    showLogo();
    $application->run();
})();
